#include "key_control.h"
#include "game.h"
#include "loop_queue.h"

LoopQueue loop_queue(2);

//设置按键
void key_pressed(unsigned char key, int x, int y) {
  loop_queue.push(key);
  // key_states[key] = true;
}

//释放按键
void key_up(unsigned char key, int x, int y) { key_states[key] = false; }

//重置所有元素并开始游戏
void resetGame() {}

void key_operations() {
  // std::cout<<"key_op1 ";
  int key = loop_queue.pop();
  // std::cout<< key << " key_op2\n";
  if (key < 0)
    return;
  switch (key) {
  case ' ':
    if (!is_play && !is_play_init) {
      start_game();
      is_play = true;
    }
    if (is_play && is_play_init) {
      if (speed_time > 10)
        speed_time = speed_time * 4 / 5;
    }
    break;
  case 'w':
    if (!(tsnake.direction_x == 0 && tsnake.direction_y == 1)) {
      tsnake.direction_x = 0;
      tsnake.direction_y = -1;
    }
    break;
  case 's':
    if (!(tsnake.direction_x == 0 && tsnake.direction_y == -1)) {
      tsnake.direction_x = 0;
      tsnake.direction_y = 1;
    }
    break;
  case 'a':
    if (!(tsnake.direction_x == 1 && tsnake.direction_y == 0)) {
      tsnake.direction_x = -1;
      tsnake.direction_y = 0;
    }
    break;
  case 'd':
    if (!(tsnake.direction_x == -1 && tsnake.direction_y == 0)) {
      tsnake.direction_x = 1;
      tsnake.direction_y = 0;
    }
    break;
  case '/':
    is_play = !is_play;
  default:
    break;
  }
}

// void key_operations() {
//   if (key_states[' ']) {
//     if (!is_play && !is_play_init) {
//       start_game();
//       is_play = true;
//     }
//     key_states[' '] = false;
//   }
//   if (key_states['w']) {
//     tsnake.direction_x = 0;
//     tsnake.direction_y = -1;
//     key_states['w'] = false;
//     // tsnake.auto_move();
//   }
//   if (key_states['s']) {
//     tsnake.direction_x = 0;
//     tsnake.direction_y = 1;
//     key_states['s'] = false;
//     // tsnake.auto_move();
//   }
//   if (key_states['a']) {
//     tsnake.direction_x = -1;
//     tsnake.direction_y = 0;
//     key_states['a'] = false;
//     // tsnake.auto_move();
//   }
//   if (key_states['d']) {
//     tsnake.direction_x = 1;
//     tsnake.direction_y = 0;
//     key_states['d'] = false;
//     // tsnake.auto_move();
//   }
// }